package com.itheima.controller;

/**
 * @author 马祥和
 * @version 1.0
 * @function
 * @time 2025/7/31 16:09
 */

import com.itheima.entity.PageBean;
import com.itheima.entity.Result;
import com.itheima.entity.Student;
import com.itheima.entity.StudentQueryParam;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RequestMapping("/students")
@RestController
public class StudentController {

    @Autowired
    private StudentService studentService;

    @GetMapping
    public Result page(StudentQueryParam sqp){
        log.info("分页查找学生:{},{},{},{},{},",sqp.getName(), sqp.getClazzId(), sqp.getDegree(), sqp.getPage(), sqp.getPageSize());
        PageBean pageBean = studentService.page(sqp);

        return Result.success(pageBean);
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable("ids") List<Integer> ids){
        log.info("ids={}", ids);
        studentService.delete(ids);
        return Result.success();
    }

    @PostMapping
    public Result insert(@RequestBody Student student){
        log.info("student={}", student);
        studentService.insert(student);
        return Result.success();
    }

    @GetMapping("{id}")
    public Result getById(@PathVariable("id") Integer id){
        log.info("查询id={}", id);

        Student student = studentService.getById(id);
        return Result.success(student);

    }

    @PutMapping
    public Result update(@RequestBody Student student){
        log.info("student={}", student);
        studentService.update(student);
        return Result.success();
    }

    @PutMapping("/violation/{id}/{score}")
    public Result violation(@PathVariable("id") Integer id, @PathVariable("score") Integer score){
        log.info("id={},score={}", id, score);
        studentService.updateScore(id,score);

        return Result.success();
    }

}
